package com.ccbooks.util.PDB;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException; //import java.util.Date;
import java.util.Date;

import com.ccbooks.bo.format.PdbBo;
import com.ccbooks.dao.BooksDao;
import com.ccbooks.dao.DBHelper;
import com.ccbooks.util.ContentDisp;
import com.ccbooks.util.MeasureText;
import com.ccbooks.util.TextSizeUtil;
import com.ccbooks.view.BookContentView;
import com.ccbooks.vo.Book;

import android.content.ContentValues;
import android.content.Context;
import android.graphics.Typeface;

//import javax.swing.JLabel;

/**
 * @author Administrator
 */

public class BooksEnginePDB {

	private final static int MAX_LINE_NUM = 1024 * 100;
	private final static int MAX_BUF_SIZE = 80;
	private final static int MAX_ZERO_NUM = 3000 ;

	private InputStreamReader inputStreamReader = null;
	private static InputStreamReader inputStreamReader2 = null;

	public int screenWidth;
	public static int[] lineIndex = new int[MAX_LINE_NUM];
	public int[] lineIndex2 = new int[MAX_LINE_NUM];
	public static int[] lineIndex3 = new int[MAX_ZERO_NUM];
	public int[] lineIndex4 = new int[MAX_LINE_NUM];
	public int curLine2 = 0;
	public int curLine4 = 0;
	public static int curLine = 0; // 文件总行数(通过计算);
	public int curLine3 = 0;
	public int totalChar; // 文件总字节数（通过计算）;
	public static String content;
	public int moveSkip = 0; // 跳转字符数；
	public int firstPageLineNum = 0; // 第一页行号数；
	public int secondPageLineNum;// 第二页行号数；
	public int thridPageLineNum;// 第三页行号数；
	public int lineCount;
	public int returnCurLine;//返回百分比计算行数；
	public static boolean fileEnd = false; 
	/**从文件开头打开*/
	public static boolean fileFirstStart = false;
	/**每页开头索引*/
	public static int markPoint;
	/**每页末尾索引*/
	public static int pageEndPoint;
	/**返回文件开头*/
	public static boolean returnFileStart = false;

	public static int pageMarkNum; // 页面首字符标记
	public int indexFont; // 字体变化后标记索引；
	public static float fileLength;//文件大小；
	private static String fileNameStr, charSetStr;
	public short status = 3;  //状态判断。1表示向前计算正在运行。2表示向前计算结束。3表示数据已连接成功；
	public boolean isMark;	//是否
	public static String str = null;
	
	
	public static void main(String args[]) {
		String content = null;
		BooksEnginePDB engine = new BooksEnginePDB();
		try {
			// engine.engineInit("c:\\text.txt", "宋体", 400, "GBK");
			engine.parseFile();
			for (int i = 0; i < 10; i++) {
				content = engine.getPageContent(252078, 10);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.print("next line content:\n");
		System.out.println(content);
	}

	public int getCharWidth(String arg) {
		MeasureText measureText = new MeasureText(TextSizeUtil.fontSize);
		return (int) measureText.getCharWidth(arg);
	}
	
	

	/**
	 * init the file & other para 文件名 字体名 宽度 编码 in: fileName, fontName, width,
	 * charSet out: void
	 */
	public void engineInit(BookContentView bcv, String fontName, int width,
			String charSet) throws FileNotFoundException,
			UnsupportedEncodingException {
		screenWidth = width;
		fileNameStr = bcv.book.bookpath;
		charSetStr = charSet;
		/*try {
			if(bcv.book.fileLength>0){
				fileLength = bcv.book.fileLength;
			}else{
				fileLength = getFileSize(fileNameStr, charSet);
				BooksDao dao =BooksDao.getDao(bcv);
				ContentValues cv = new ContentValues();
				cv.put(DBHelper.FILE_LENGTH, fileLength);
				dao.update(DBHelper.TABLE_BOOK, bcv.book.id,cv );
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		inputStreamReader = new InputStreamReader(
				new FileInputStream(fileNameStr), charSet);

		// 修改为与平台有关的
		/*
		 * font = new Font("宋体", Font.BOLD, 12); fontMetrics =
		 * gc.getFontMetrics(font);
		 */

		//font = new Font(Typeface.create(fontName, Typeface.BOLD), fontSize);
		bcv.bcHandler.sendEmptyMessage(3);
		return;
	}

	/**
	 * 获得所有字符数
	 * analysis file
	 */
	public int getLineCount(int fontSize) {
		TextSizeUtil textSizeUtil = new TextSizeUtil();
		lineCount = textSizeUtil.fontSize;
		return lineCount;
	}

	public void parseFile() throws IOException {
		int readNum;
		int width = 0;
		String newString = null, oldString = new String("");
		int index = 0;
		boolean hasEnter = false;
		boolean hasEnoughWidth = false;

		int countCharNum = 0;

		lineIndex[0] = 0;
		while (true) {
			char buf[] = new char[MAX_BUF_SIZE];//
			readNum = inputStreamReader.read(buf, 0, MAX_BUF_SIZE);

			// file end
			if (readNum == -1) {
				break;
			}

			countCharNum += readNum;

			newString = new String(buf);

			// 读取一次的数据
			while (true) {
				newString = oldString + newString;
				oldString = new String("");
				width = 0;
				hasEnter = false;
				hasEnoughWidth = false;
				for (index = 0; index < newString.length(); index++) {
					// string 's width > screenWidth
					if (width > screenWidth) {
						hasEnoughWidth = true;
						break;
					}
					if (newString.charAt(index) == '\n') {
						hasEnter = true;
						break;
					}
					String text = newString.substring(index, index + 1);
					System.out.println("test-------------->" + text);
					width += getCharWidth(text);
				}

				// hasEnter || hasEnoughWidth
				if (hasEnter || hasEnoughWidth) {
					lineIndex[curLine + 1] = lineIndex[curLine] + index + 1;
					curLine++;
					index++;
					newString = newString.substring(index);
				}
				// don't have enough width
				else {
					oldString = newString;
					break;
				}
			}
		}

		this.totalChar = countCharNum;
	}

	/**
	 * 第三步调用，得到数据 get the page content in: page_no
	 */
	public String getPageContent(int lineNo, int lineCount)
			throws IndexOutOfBoundsException, IOException {
		StringBuffer strBuffer = new StringBuffer();
		long skipNum, retNum;
		int readNum;
		String Str = "    ";
		String tempStr ;
		boolean lineEndFlag = false;
		int j = 0;
		System.out.println("getPageContent");
		System.out.println("lineNo"+lineNo);
		System.out.println("lineIndex[lineNo]"+lineIndex[lineNo]);
		System.out.println("curLine"+curLine);
		System.out.println("lineCount"+lineCount);
		if (lineNo >= curLine)
			return content = null;

		// reset the stream
		
		// skip the stream count n char
		
		skipNum = lineIndex[lineNo];
		System.out.println("Str length"+str.length());
		//str = str.substring((int) skipNum);
		System.out.println("Str length"+str.length());
		// get content

		for (int i = 0; i < lineCount; i++) {
		
			
			String subStr = null;
			
			if (lineNo + i +1> curLine) {
				
				break;
			}
			System.out.println("lineIndex[lineNo + i]"+(lineNo + i));
			System.out.println("lineIndex[lineNo + i + 1]"+(lineNo + i + 1));
			System.out.println("Str lineNo"+lineNo);
//			for(int t = 0;t<=curLine;t++){
//				System.out.println("lineIndex"+t+"="+lineIndex[t]);
//			}
			subStr =  str.substring(lineIndex[lineNo + i], lineIndex[lineNo + i + 1]);
			readNum = str.substring(lineIndex[lineNo + i], lineIndex[lineNo + i + 1]).length();
			
			
			if (readNum == -1){
				break;
			}
/*			if (buf[readNum - 1] == '\n') {
				lineEndFlag = true;
				// str += (new String(buf, 0, readNum));
				strBuffer.append(buf, 0, readNum);
			} else {
				if(lineEndFlag == true){
					
					
					strBuffer.append(Str);
					lineEndFlag = false;
				}else{
				// str += ((new String(buf, 0, readNum)) + '\n');
				strBuffer.append(buf, 0, readNum);
				strBuffer.append('\n');
				}
			}*/
			
			 if (subStr.charAt(readNum-1)=='\n'){
					//str += (new String(buf, 0, readNum));
					strBuffer.append(subStr);
				
					if(j == 0){
						markPoint = lineIndex[lineNo + i + 1];
						System.out.println("markPoint-----------------------"+markPoint);
					}
					j++;
					
				} else {
					//str += ((new String(buf, 0, readNum)) + '\n');
					strBuffer.append(subStr);
					strBuffer.append('\n');
				
				}

		}
		
		System.out.println("content++++++lineIndex[lineNo]++++++++++>" + lineIndex[lineNo]);
		System.out.println("content++++++lineIndex["+lineNo+"]++++++++++>" + lineIndex[lineNo]);
		
	
		pageEndPoint = lineIndex[lineNo+lineCount-1];
		content = new String(strBuffer);
		content =content.replace("\r",""); 
		return content;
	}

	/**
	 * 通过百分比得到数据
	 * */


	public String getMiddlePageContent(int lineCount)
			throws IndexOutOfBoundsException, IOException {
		String str = "";
		long retNum;
		int readNum;
		int lineNo = 0;
	
		if (lineNo >= curLine)
			return "";
	
		// reset the stream
		inputStreamReader = new InputStreamReader(new FileInputStream(
				fileNameStr), charSetStr);
	
		// skip the stream count n char
		// skipNum = lineIndex[lineNo];
		do {
			retNum = inputStreamReader.skip(moveSkip);
			moveSkip -= retNum;
		} while (moveSkip != 0);
	
		// get content
	
		for (int i = 0; i < lineCount; i++) {
			char buf[] = new char[MAX_BUF_SIZE];
	
			if (lineNo + i + 1 > curLine)
				break;
			// System.out.println("cruLine--"+i+"--lineIndex---:" +lineNo + i+1
			// );
			readNum = inputStreamReader.read(buf, 0, lineIndex[lineNo + i + 1]
					- lineIndex[lineNo + i]);
	
			if (readNum == -1)
				break;
	
			if (buf[readNum - 1] == '\n') {
				str += (new String(buf, 0, readNum));
			} else {
				str += ((new String(buf, 0, readNum)) + '\n');
			}
		}
		return content = str;
	}

	/**
	 * 第二步调用
	 * 
	 * @param persent
	 *            百分比 开始第一页为0 获得
	 * */
	public void parseFileByPercent(String fileNameStr, int persent,
			int lineCount) throws IOException {

		int readNum;
		int width = 0;
		float temppersent;
		
		float tempnumber;
		long skipNumber =0;
		int searchNumber = 0;
		String newString = null, oldString = new String("");
		int index = 0;
		boolean hasEnter = false;
		boolean hasEnoughWidth = false;

		int countCharNum = 0;

		int i = 0;
		
		
		
		
		lineIndex[0] = 0;
		curLine = 0;
		if (persent != 0) {
			fileLength = str.length();
			System.out.println("fileLength" + fileLength);
			temppersent = (float) persent / 100.0f;
			System.out.println("persent" + persent);
			tempnumber = fileLength * temppersent;
			// System.out.println("tempnumber" + tempnumber);
			skipNumber = Math.round(tempnumber);
			// System.out.println("skipNumber" + skipNumber);
			String substrs = str.substring((int) skipNumber);
			int j = 0;
			System.out.println("--------while (true) {-------start----------");
			while (true) {
				
				 char bubstr = substrs.charAt(j);
				System.out.println("bubstr"+bubstr);
				searchNumber++;
				
				if (bubstr =='\n') {

					break;
				}
				if ( j>= substrs.length()-1) {

					break;
				}
				j++;
			}
			
			moveSkip = (int) skipNumber + searchNumber;
			
			lineIndex[0] = moveSkip;
		}
		String substr = str.substring(moveSkip);
		System.out.println("--------while (true) {------end-----------");
		//moveSkip = (int)skipNumber + searchNumber;
		System.out.println("--------moveSkip {------end-----------"+moveSkip);
		
		while (true) {
			if(i == substr.length()/MAX_BUF_SIZE){
				break;
			}
			newString = substr.substring(i*MAX_BUF_SIZE, (i+1)*MAX_BUF_SIZE);
		

			// 读取一次的数据
			while (true) {
				newString = oldString + newString;
				oldString = new String("");
				width = 0;
				if (hasEnter == true){
					width = TextSizeUtil.fontSize;
				}
				hasEnter = false;
				hasEnoughWidth = false;
				
				for (index = 0; index < newString.length(); index++) {
					String text = newString.substring(index, index + 1);

					//if (newString.charAt(index) != '\r') {
						width += getCharWidth(text);
					//}
					// string 's width > screenWidth
					if (width > screenWidth - TextSizeUtil.fontSize) {
						hasEnoughWidth = true;
						break;
					}
					if (newString.charAt(index) == '\n') {
						hasEnter = true;
						break;
					}

				
				}

				// hasEnter || hasEnoughWidth
				
					if (hasEnter || hasEnoughWidth) {

						lineIndex[curLine + 1] = lineIndex[curLine] + index + 1;

						curLine++;
						index++;
						newString = newString.substring(index);

					} else {
						oldString = newString;
						break;
					}
				//当读取出来当前一页的内容时候，返回startDisp值，让显示线程开始启动；
				if(curLine == lineCount){
					ContentDisp.startDisp = true;
				}
				if (curLine >= 10*lineCount) {
					moveSkip = moveSkip + lineIndex[curLine + 1];
				//	lineIndex[1] = lineIndex[0]+1;
					System.out.println("---------------------lineIndex[curLine]------------"+lineIndex[curLine]);
					System.out.println("---------------------lineIndex[curLine + 1]------------"+lineIndex[curLine + 1]);
					System.out.println("---------------------ParseEnd------------"
							+ curLine);
					//this.returnCurLine = curLine;
					for(int s = 0; s<=curLine ;s++){
						System.out.println("lineIndex"+s+"="+lineIndex[s]);
					}
					this.pageMarkNum = lineIndex[0];
					System.out.println("lineIndex[1];"+lineIndex[1]);
					moveSkip = moveSkip + lineIndex[curLine + 1];
					this.totalChar = countCharNum;
					return;
				}
			}
			i++;
		}
		
		return;
	}

	/**文件首次打开是解析方法*/
	public void parseFileFirst(String fileNameStr, int lineCount,String charSet) throws IOException {

		int readNum;
		int width = 0;
	
		String newString = null, oldString = new String("");
		int index = 0;
		boolean hasEnter = false;
		boolean hasEnoughWidth = false;
		fileFirstStart = true;
		int countCharNum = 0;
		System.out.println("--------while (true) {------end-----------");
		//moveSkip = (int)skipNumber + searchNumber;
		System.out.println("--------moveSkip {------end-----------"+moveSkip);
		PdbBo  pdbBo = new PdbBo(fileNameStr, charSet);
		str = pdbBo.showStr();
		int i = 0;
		lineIndex[0] = 0;
		curLine = 0;
		while (true) {
			if(i == str.length()/MAX_BUF_SIZE){
				break;
			}
			newString = str.substring(i*MAX_BUF_SIZE, (i+1)*MAX_BUF_SIZE);
			// 读取一次的数据
			while (true) {
				newString = oldString + newString;
				oldString = new String("");
				width = 0;
				if (hasEnter == true){
					width = TextSizeUtil.fontSize;
				}
				hasEnter = false;
				hasEnoughWidth = false;
				
				for (index = 0; index < newString.length(); index++) {
					String text = newString.substring(index, index + 1);

					//if (newString.charAt(index) != '\r') {
						width += getCharWidth(text);
					//}
					// string 's width > screenWidth
					if (width > screenWidth - TextSizeUtil.fontSize) {
						hasEnoughWidth = true;
						break;
					}
					if (newString.charAt(index) == '\n') {
						hasEnter = true;
						break;
					}

					
				}

				// hasEnter || hasEnoughWidth
				
				if (hasEnter || hasEnoughWidth) {

					lineIndex[curLine + 1] = lineIndex[curLine] + index + 1;

					curLine++;
					index++;
					newString = newString.substring(index);

				} else {
					oldString = newString;
					break;
				}
				//当读取出来当前一页的内容时候，返回startDisp值，让显示线程开始启动；
				if(curLine == lineCount){
					ContentDisp.startDisp = true;
				}
				if (curLine >= 10*lineCount) {
					
					moveSkip = moveSkip + lineIndex[curLine + 1];
					System.out.println("---------------------lineIndex[curLine]------------"+lineIndex[curLine]);
					System.out.println("---------------------lineIndex[curLine + 1]------------"+lineIndex[curLine + 1]);
					System.out.println("---------------------ParseEnd------------"
							+ curLine);
					for(int s = 0; s<=curLine ;s++){
						System.out.println("lineIndex"+s+"="+lineIndex[s]);
					}
					//this.returnCurLine = curLine;
					this.pageMarkNum = lineIndex[0];
					System.out.println("lineIndex[1];"+lineIndex[1]);
					moveSkip = moveSkip + lineIndex[curLine + 1];
					this.totalChar = countCharNum;
					return;
				}
			}
			i++;
		}
		
		return;
	}

	
	public void parseFileBookMark(String fileNameStr, int markPoint,
			int lineCount) throws IOException {

		int readNum;
		int width = 0;
		long skipNumber =0;
		
		String newString = null, oldString = new String("");
		int index = 0;
		boolean hasEnter = false;
		boolean hasEnoughWidth = false;
        
		int countCharNum = 0;
		moveSkip = markPoint;
		
		int i = 0;
		
		
		
		String substr = str.substring(moveSkip);

		System.out.println("--------while (true) {------end-----------");
		lineIndex[0] = moveSkip;
		curLine = 0;
		System.out.println("--------moveSkip {------end-----------"+moveSkip);
		while (true){ 
			if(i == substr.length()/MAX_BUF_SIZE){
				break;
			}
			newString = substr.substring(i*MAX_BUF_SIZE, (i+1)*MAX_BUF_SIZE);
		
			// 读取一次的数据
			while (true) {
				newString = oldString + newString;
				oldString = "";
				width = 0;
				if (hasEnter == true){
					width = TextSizeUtil.fontSize;
				}
				hasEnter = false;
				hasEnoughWidth = false;
				
				for (index = 0; index < newString.length(); index++) {
					String text = newString.substring(index, index + 1);

					//if (newString.charAt(index) != '\r') {
						width += getCharWidth(text);
					//}
					// string 's width > screenWidth
					if (width > screenWidth - TextSizeUtil.fontSize) {
						hasEnoughWidth = true;
						break;
					}
					if (newString.charAt(index) == '\n') {
						hasEnter = true;
						break;
					}

					
				}

				// hasEnter || hasEnoughWidth
				
				if (hasEnter || hasEnoughWidth) {

					lineIndex[curLine + 1] = lineIndex[curLine] + index + 1;

					curLine++;
					index++;
					newString = newString.substring(index);

				} else {
					oldString = newString;
					break;
				}
				//当读取出来当前一页的内容时候，返回startDisp值，让显示线程开始启动；
				if(curLine == lineCount){
					ContentDisp.startDisp = true;
				}
				if (curLine >= 10*lineCount) {
					moveSkip = moveSkip + lineIndex[curLine + 1];
					System.out.println("---------------------lineIndex[curLine]------------"+lineIndex[curLine]);
					System.out.println("---------------------lineIndex[curLine + 1]------------"+lineIndex[curLine + 1]);
					System.out.println("---------------------ParseEnd------------"
							+ curLine);
					this.returnCurLine = curLine;
					this.pageMarkNum = lineIndex[0];
					System.out.println("lineIndex[1];"+lineIndex[1]);
					moveSkip = moveSkip + lineIndex[curLine + 1];
					this.totalChar = countCharNum;
					return;
				}
			}
			i++;
		}
		
		return;
	}

	
	//解析字体大小改变；
	public void parseFileContentSize(String fileNameStr, int lineCount) throws IOException {

		int readNum;
		int width = 0;
		long skipNumber =0;
		
		String newString = null, oldString = new String("");
		int index = 0;
		boolean hasEnter = false;
		boolean hasEnoughWidth = false;
       
		int countCharNum = 0;
		
		int i = 0;
	
		String substr = str.substring(markPoint);
		
		
		
		System.out.println("--------while (true) {------end-----------");
		System.out.println("markPoint"+markPoint);
		lineIndex[0] = markPoint;
		
		curLine = 0;
		System.out.println("--------moveSkip {------end-----------"+moveSkip);
		
		while (true){ 
			if(i == substr.length()/MAX_BUF_SIZE){
				break;
			}
			newString = substr.substring(i*MAX_BUF_SIZE, (i+1)*MAX_BUF_SIZE);
			// 读取一次的数据
			while (true) {
				newString = oldString + newString;
				oldString = new String("");
				width = 0;
				if (hasEnter == true){
					width = TextSizeUtil.fontSize;
				}
				hasEnter = false;
				hasEnoughWidth = false;
				
				for (index = 0; index < newString.length(); index++) {
					
					String text = newString.substring(index, index + 1);

					//if (newString.charAt(index) != '\r') {
					
						width += getCharWidth(text);
						
					//}
					// string 's width > screenWidth
					if (width > screenWidth - TextSizeUtil.fontSize) {
						hasEnoughWidth = true;
						break;
					}
					if (newString.charAt(index) == '\n') {
						hasEnter = true;
						break;
					}
					
					
				}

				// hasEnter || hasEnoughWidth
				
					if (hasEnter || hasEnoughWidth) {

						lineIndex[curLine + 1] = lineIndex[curLine] + index + 1;

						curLine++;
						index++;
						newString = newString.substring(index);

					} else {
						oldString = newString;
						break;
					}
				//当读取出来当前一页的内容时候，返回startDisp值，让显示线程开始启动；
				if(curLine == lineCount){
					ContentDisp.startDisp = true;
				}
				if (curLine >= 10*lineCount) {
					moveSkip = moveSkip + lineIndex[curLine + 1];
					System.out.println("---------------------lineIndex[curLine]------------"+lineIndex[curLine]);
					System.out.println("---------------------lineIndex[curLine + 1]------------"+lineIndex[curLine + 1]);
					System.out.println("---------------------ParseEnd------------"
							+ curLine);
					this.returnCurLine = curLine;
					this.pageMarkNum = lineIndex[0];
					for(int s = 0; s <=curLine;s++ ){
						System.out.println(s+"==!!!!!="+lineIndex[s]);
					}
					System.out.println("lineIndex[1];"+lineIndex[1]);
					moveSkip = moveSkip + lineIndex[curLine + 1];
					this.totalChar = countCharNum;
					return;
				}
			}
			i++;
		}
		
		return;
	}


	/**
	 * 读完了getContent之后下一页
	 * 
	 * */
	public void parseFileNextPage(String fileNameStr, int lineCount)
			throws IOException {

		int readNum;
		int width = 0;
		
		String newString = null, oldString = new String("");
		int index = 0;
		boolean hasEnter = false;
		boolean hasEnoughWidth = false;

		int countCharNum = 0;
		inputStreamReader.skip(moveSkip);
		lineIndex[0] = 0;
		while (true) {
			char buf[] = new char[MAX_BUF_SIZE];//
			readNum = inputStreamReader.read(buf, 0, MAX_BUF_SIZE);

			// file end
			if (readNum == -1) {
				break;
			}

			countCharNum += readNum;

			newString = new String(buf);

			// 读取一次的数据
			while (true) {
				newString = oldString + newString;
				oldString = new String("");
				width = 0;
				if (hasEnter == true){
					width = TextSizeUtil.fontSize;
				}
				hasEnter = false;
				hasEnoughWidth = false;
				for (index = 0; index < newString.length(); index++) {
					// string 's width > screenWidth
					if (width > screenWidth) {
						hasEnoughWidth = true;
						break;
					}
					if (newString.charAt(index) == '\n') {
						hasEnter = true;
						break;
					}
					String text = newString.substring(index, index + 1);
					width += getCharWidth(text);
				}

				// hasEnter || hasEnoughWidth
				if (hasEnter || hasEnoughWidth) {
					lineIndex[curLine + 1] = lineIndex[curLine] + index + 1;
					curLine++;
					index++;
					newString = newString.substring(index);
				}
				// don't have enough width
				else {
					oldString = newString;
					break;
				}
				if (curLine >= lineCount) {
					break;
				}
			}
		}

		moveSkip = moveSkip + lineIndex[curLine + 1];
		this.totalChar = countCharNum;

	}

	// 字体调整转换
	public void parseFileReturnPage(String fileNameStr, int lineCount)
			throws IOException {

		int readNum;
		int width = 0;
		String newString = null, oldString = new String("");
		int index = 0;
		boolean hasEnter = false;
		boolean hasEnoughWidth = false;

		/*
		 * pageMarkNum = pageMarkNum-10000; if(pageMarkNum<=0){ pageMarkNum =
		 * pageMarkNum }
		 */
		if (pageMarkNum > 10000) {
			pageMarkNum -= 10000;
		} else if (pageMarkNum <= 10000 && pageMarkNum > 5000) {
			pageMarkNum -= 5000;
		} else if (pageMarkNum <= 5000 && pageMarkNum > 3000) {
			pageMarkNum -= 3000;
		} else if (pageMarkNum <= 3000 && pageMarkNum > 1500) {
			pageMarkNum -= 1500;
		} else if (pageMarkNum <= 1500 && pageMarkNum > 150) {
			pageMarkNum -= 150;
		}

		int countCharNum = 0;
		inputStreamReader.skip(pageMarkNum);
		lineIndex[0] = 0;
		while (true) {
			char buf[] = new char[MAX_BUF_SIZE];//
			readNum = inputStreamReader.read(buf, 0, MAX_BUF_SIZE);

			// file end
			if (readNum == -1) {
				break;
			}

			countCharNum += readNum;

			newString = new String(buf);
			if (countCharNum >= 9980 && countCharNum <= 10060) {
				indexFont = curLine + 1;
			}
			// 读取一次的数据
			while (true) {
				newString = oldString + newString;
				oldString = new String("");
				width = 0;
				hasEnter = false;
				hasEnoughWidth = false;

				for (index = 0; index < newString.length(); index++) {
					// string 's width > screenWidth
					if (width > screenWidth) {
						hasEnoughWidth = true;
						break;
					}
					if (newString.charAt(index) == '\n') {
						hasEnter = true;
						break;
					}
					// width += getCharWidth(newString.charAt(index));
				}

				// hasEnter || hasEnoughWidth
				if (hasEnter || hasEnoughWidth) {
					lineIndex[curLine + 1] = lineIndex[curLine] + index + 1;
					curLine++;
					index++;
					newString = newString.substring(index);
				}
				// don't have enough width
				else {
					oldString = newString;
					break;
				}
				if (curLine >= lineCount) {
					break;
				}
			}
		}

		moveSkip = moveSkip + lineIndex[curLine + 1];
		this.totalChar = countCharNum;
	}

	// 向前返回
	public void parseFileReturna(String fileNameStr, int lineCount)
			throws IOException {
		System.out.println("-------------start return--------ParseEnd------------");
		int readNum;
		int width = 0;
		String newString = null, oldString = new String("");
		int index = 0;
		boolean hasEnter = false;
		boolean hasEnoughWidth = false;
		inputStreamReader2 = new InputStreamReader(
				new FileInputStream(fileNameStr), charSetStr);
		/*
		 * pageMarkNum = pageMarkNum-10000; if(pageMarkNum<=0){ pageMarkNum =
		 * pageMarkNum }
		 */
		int pageMarkNum1 = pageMarkNum;
		System.out.println("-------------pageMarkNum--------ParseEnd------------"+pageMarkNum);
	
		if (pageMarkNum1 > 5000) {
			pageMarkNum1 -= 5000;
			System.out.println("***********pageMarkNum1*****************"+pageMarkNum1);
		} else if (pageMarkNum1 <= 5000 && pageMarkNum1 > 3000) {
			pageMarkNum1 -= 3000;
		} else if (pageMarkNum1 <= 3000 && pageMarkNum1 > 1500) {
			pageMarkNum1 -= 1500;
			System.out.println("***********pageMarkNum1*****************"+pageMarkNum1);
		} else if (pageMarkNum1 <= 1500 && pageMarkNum1 > 150) {
			pageMarkNum1 -= 150;
			System.out.println("***********pageMarkNum1*****************"+pageMarkNum1);
		} else{
			pageMarkNum1 = 0;
		}
		System.out.println("-------------pageMarkNum1--------ParseEnd------------"+pageMarkNum1);
		int countCharNum = 0;
		/*returnFileStart = false;
		if(pageMarkNum1 <= 1500){
			pageMarkNum1 = 0;
			returnFileStart = true;
		}*/
		inputStreamReader2.skip(pageMarkNum1);
		int searchNumber = 0;
		while (true) {

			int i;
			
			char buf1[] = new char[1];
			readNum = inputStreamReader2.read(buf1, 0, 1);
			
			if (buf1[0] == '\n') {
				
				break;
			}
			if (readNum == -1) {

				break;
			}
			searchNumber++;
		}
		System.out.println("-------------pageMarkNum1--------ParseEnd------------"+pageMarkNum1);
		moveSkip =  (int)pageMarkNum1 + searchNumber;
		int moveCount = (int)pageMarkNum1 + searchNumber;
		lineIndex2 = new int[MAX_LINE_NUM];
		lineIndex2[0] = moveCount;
		curLine2 = 0;
		while (true) {
			char buf[] = new char[MAX_BUF_SIZE];//
			readNum = inputStreamReader2.read(buf, 0, MAX_BUF_SIZE);

			// file end
			if (readNum == -1) {
				this.status = 2;
				System.out.println("curLine2----readNum == -1-------->"+curLine2);
				break;
			}
			readNum +=readNum;
			
			countCharNum += readNum;

			newString = new String(buf);
			
			// 读取一次的数据
			while (true) {
				
				
				
				newString = oldString + newString;
				oldString = new String("");
				width = 0;
				hasEnter = false;
				hasEnoughWidth = false;
				moveCount = moveCount+index; 
				
				for (index = 0; index <newString.length(); index++) {
				//	System.out.println("-------------start return--------index------------"+index);						
					// string 's width > screenWidth
					
					
					if (width > screenWidth - 5) {
					
						hasEnoughWidth = true;
						break;
					}
					
					
					
					if (newString.charAt(index) == '\n') {
					
						hasEnter = true;
						break;
					}
					
					String text = newString.substring(index, index + 1);

				//	if (newString.charAt(index) != '\r') {
						width += getCharWidth(text);
				//	}
					
					
					
				}

				// hasEnter || hasEnoughWidth
				
					if (hasEnter || hasEnoughWidth) {

						lineIndex2[curLine2 + 1] = lineIndex2[curLine2] + index + 1;
System.out.println("return lineIndex2"+lineIndex2[curLine2 + 1]);
						curLine2++;
						
						index++;
						newString = newString.substring(index);

					} else {
						oldString = newString;
						break;
					}
					
					
				
					if (lineIndex2[curLine2] >= pageMarkNum) {
			        	   
						this.status = 2;
						moveSkip = moveSkip + lineIndex2[curLine2 + 1];
						return;
		           }   
				
			}
			
			
		}
		
	
		//moveSkip = moveSkip + lineIndex[curLine + 1];
		this.totalChar = countCharNum;

	}
	
	
	public void parseFileReturn(String fileNameStr,	int lineCount) throws IOException {

		int readNum;
		int width = 0;

		int searchNumber = 0;
		String newString = null, oldString = new String("");
		int index = 0;
		boolean hasEnter = false;
		boolean hasEnoughWidth = false;

		int countCharNum = 0;
		
		
		int pageMarkNum1 = pageMarkNum;
		System.out.println("-------------pageMarkNum--------ParseEnd------------"+pageMarkNum);
	
		if (pageMarkNum1 > 5000) {
			pageMarkNum1 -= 5000;
			System.out.println("***********pageMarkNum1*****************"+pageMarkNum1);
		} else if (pageMarkNum1 <= 5000 && pageMarkNum1 > 3000) {
			pageMarkNum1 -= 3000;
		} else if (pageMarkNum1 <= 3000 && pageMarkNum1 > 1500) {
			pageMarkNum1 -= 1500;
			System.out.println("***********pageMarkNum1*****************"+pageMarkNum1);
		} else if (pageMarkNum1 <= 1500 && pageMarkNum1 > 150) {
			pageMarkNum1 -= 150;
			System.out.println("***********pageMarkNum1*****************"+pageMarkNum1);
		} else{
			pageMarkNum1 = 0;
		}
	
	
		String substrs = str.substring(pageMarkNum1);
		int i = 0;
		int j = 0;
		System.out.println("--------while (true) {-------start----------");
		while (true) {
			
			 char bubstr = substrs.charAt(j);
			System.out.println("bubstr"+bubstr);
			searchNumber++;
			
			if (bubstr =='\n') {

				break;
			}
			if ( j>= substrs.length()-1) {

				break;
			}
			j++;
		}
		
			
			int moveSkips = (int) pageMarkNum1 + searchNumber;
			lineIndex2 = new int[MAX_LINE_NUM];
			String substr = str.substring(moveSkips);
			curLine2 = 0;
			lineIndex2[0] = moveSkips;
			
		System.out.println("--------while (true) {------end-----------");
		//moveSkip = (int)skipNumber + searchNumber;
		System.out.println("--------moveSkip {------end-----------"+moveSkip);
		
		while (true) {
			if(i == substr.length()/MAX_BUF_SIZE){
				break;
			}
			newString = substr.substring(i*MAX_BUF_SIZE, (i+1)*MAX_BUF_SIZE);
			// 读取一次的数据
			while (true) {
				newString = oldString + newString;
				oldString = new String("");
				width = 0;
				if (hasEnter == true){
					width = TextSizeUtil.fontSize;
				}
				hasEnter = false;
				hasEnoughWidth = false;
				
				for (index = 0; index < newString.length(); index++) {
					String text = newString.substring(index, index + 1);

					//if (newString.charAt(index) != '\r') {
						width += getCharWidth(text);
					//}
					// string 's width > screenWidth
					if (width > screenWidth - TextSizeUtil.fontSize) {
						hasEnoughWidth = true;
						break;
					}
					if (newString.charAt(index) == '\n') {
						hasEnter = true;
						break;
					}

				
				}

				// hasEnter || hasEnoughWidth
				
					if (hasEnter || hasEnoughWidth) {

						lineIndex2[curLine2 + 1] = lineIndex2[curLine2] + index + 1;

						curLine2++;
						index++;
						newString = newString.substring(index);

					} else {
						oldString = newString;
						break;
					}
				
					if (lineIndex2[curLine2] >= pageMarkNum) {
			        	   
						this.status = 2;
						//moveSkip = moveSkip + lineIndex2[curLine2 + 1];
						return;
		           }   
			}
			i++;
		}
		
		return;
	}

	//计算接下来的行数
	public void parseFileNext(String fileNameStr,int lineCount) throws IOException {
		System.out.println("---------------------nextStart------------");
		int readNum;
		int width = 0;
		System.out.println("---nextStart---curLine---------"+curLine);

		System.out.println("---------------------nextStart------------");
		int shipPoint = this.lineIndex[curLine];
		System.out.println("--nextStart------"+(curLine-2)+"--shipPoint----"+lineIndex[curLine-2]);
		System.out.println("--nextStart------"+(curLine-1)+"--shipPoint----"+lineIndex[curLine-1]);
		System.out.println("--nextStart------"+(curLine)+"--shipPoint----"+lineIndex[curLine]);
		System.out.println("--nextStart--------shipPoint----"+lineIndex[curLine+1]);
		System.out.println("--nextStart--------shipPoint----"+lineIndex[curLine+2]);
		
		String newString = null, oldString = new String("");
		int tempCurLines = curLine;
		int index = 0;
		boolean hasEnter = false;
		boolean hasEnoughWidth = false;
		int tempCurLine = 10*lineCount+curLine;
		
		int countCharNum = 0;

		
		int i = 0;
		
		// fileLength = fis.length();
		System.out.println("lineIndex[1];"+lineIndex[1]);
		System.out.println("--nextStart--------shipPoint----"+shipPoint);
		String substr = null;
		substr = str.substring(shipPoint);
		curLine = this.curLine;
		int oldCurLine = this.curLine;
		lineIndex[curLine] = lineIndex[curLine];
		
		
		while (true) {
			if(i == substr.length()/MAX_BUF_SIZE){
				break;
			}
			newString = substr.substring(i*MAX_BUF_SIZE, (i+1)*MAX_BUF_SIZE);
			// 读取一次的数据
			while (true) {
				newString = oldString + newString;
				oldString = new String("");
				width = 0;
				if (hasEnter == true){
					width = TextSizeUtil.fontSize;
				}
				hasEnter = false;
				hasEnoughWidth = false;
				
				for (index = 0; index < newString.length(); index++) {
					String text = newString.substring(index, index + 1);

					//if (newString.charAt(index) != '\r') {
						width += getCharWidth(text);
					//}
					// string 's width > screenWidth
					if (width > screenWidth - TextSizeUtil.fontSize) {
						hasEnoughWidth = true;
						break;
					}
					if (newString.charAt(index) == '\n') {
						hasEnter = true;
						break;
					}

					
				}

				// hasEnter || hasEnoughWidth
					
					
			
					if (hasEnter || hasEnoughWidth) {
						System.out.println("curLine"+curLine);
						System.out.println("lineIndex[curLine]"+lineIndex[curLine]);
						lineIndex[curLine + 1] = lineIndex[curLine] + index + 1;
						System.out.println("lineIndex[curLine]"+lineIndex[curLine+1]);
						curLine++;
						
						index++;
						newString = newString.substring(index);

					} else {
						oldString = newString;
						break;
					}
					
					
				
			    
				if (curLine >= tempCurLine) {
					lineIndex[oldCurLine+1] = lineIndex[oldCurLine+1]+1;
					moveSkip = moveSkip + lineIndex[curLine + 1];
					System.out.println("---------------------lineIndex[curLine]------------"+lineIndex[curLine]);
					System.out.println("---------------------lineIndex[curLine + 1]------------"+lineIndex[curLine + 1]);
					System.out.println("---------------------nextEnd------------"
							+ curLine);
					for(int s = 0; s<=curLine ;s++){
						System.out.println("lineIndex"+s+"="+lineIndex[s]);
					}
					this.returnCurLine = curLine;
					this.pageMarkNum = lineIndex[1];
					System.out.println("lineIndex[1];"+lineIndex[1]);
					moveSkip = moveSkip + lineIndex[curLine + 1];
					this.totalChar = countCharNum;
					return;
				}
			}
			i++;
		}
		
		return;
	}
	/**
	 * 拖动条至百分百位置显示
	 * */
	public void parseFileMax(String fileNameStr, int lineCount)
	throws IOException {

		
		int width = 0;
		
		int searchNumber = 0;
		String newString = null, oldString = new String("");
		int index = 0;
		boolean hasEnter = false;
		boolean hasEnoughWidth = false;
		
		
		int shipCount = str.length() - 3000;
		if(shipCount <= 0){
			shipCount = 0;
		}
		
		
		
		System.out.println("--------while (true) {-------start----------");
		String substrs = str.substring( shipCount);
		int j = 0;
		System.out.println("--------substrs.length------"+substrs.length());
		while (true) {
			
			 char bubstr = substrs.charAt(j);
			System.out.println("bubstr"+bubstr);
			searchNumber++;
			
			if (bubstr =='\n') {

				break;
			}
			if ( j>= substrs.length()-1) {

				break;
			}
			j++;
		}
		
		int moveSkips = (int) shipCount + searchNumber;
		String substr = str.substring(moveSkips);
		System.out.println("--------substr.length------"+substr.length());
		System.out.println("--------moveSkips------"+moveSkips);
		System.out.println("--------str.length------"+str.length());
		lineIndex4 = new int[MAX_LINE_NUM];
		curLine4 = 0;
		curLine = lineCount;
		lineIndex4[0] = moveSkips;
		
		
		System.out.println("--------while (true) {------end-----------");
		//moveSkip = (int)skipNumber + searchNumber;
		System.out.println("--------moveSkip {------end-----------"+moveSkip);
		int i = 0;
		boolean flags = false;
		while (true) {
			if(i == substr.length()/MAX_BUF_SIZE-1){
				
				newString = substr.substring(i*MAX_BUF_SIZE, substr.length());
				flags = true;
				
			}else if(i != substr.length()/MAX_BUF_SIZE-1){
			
				newString = substr.substring(i*MAX_BUF_SIZE, (i+1)*MAX_BUF_SIZE);
			}

		
			// 读取一次的数据
			while (true) {
				newString = oldString + newString;
				oldString = new String("");
				width = 0;
				if (hasEnter == true) {
					width = TextSizeUtil.fontSize;
				}
				hasEnter = false;
				hasEnoughWidth = false;
		
				for (index = 0; index < newString.length(); index++) {
					String text = newString.substring(index, index + 1);
		
					// if (newString.charAt(index) != '\r') {
					width += getCharWidth(text);
					// }
					// string 's width > screenWidth
					if (width > screenWidth - TextSizeUtil.fontSize) {
						hasEnoughWidth = true;
						break;
					}
					if (newString.charAt(index) == '\n') {
						hasEnter = true;
						break;
					}
		
				}
		
				// hasEnter || hasEnoughWidth
		
				if (hasEnter || hasEnoughWidth) {
					
					lineIndex4[curLine4 + 1] = lineIndex4[curLine4] + index + 1;
					System.out.println("lineIndex4"+(curLine4 + 1)+"===="+lineIndex4[curLine4 + 1]);
					curLine4++;
					index++;
					newString = newString.substring(index);
		
				} else {
					oldString = newString;
					
					System.out.println("-------------break------------------");
					break;
				}
		
				
			}
			if(flags){
				flags = false;
				int temp = curLine4;
				System.out.println("temp"+temp);
				
				int pageCount = temp/lineCount;
				int remainder = temp%lineCount;
				int pageIndex = remainder+pageCount*(lineCount);
				
				
				
				
				//
				System.arraycopy(lineIndex4, pageIndex-lineCount, lineIndex, 0,lineCount+1);
				for(int s=0;s<curLine4+2;s++){
					System.out.println("index4="+s+"="+lineIndex4[s]);
				}
				for(int s=0;s<curLine4+2;s++){
					System.out.println("index4="+s+"="+lineIndex[s]);
				}
				ContentDisp.startDisp = true;
				this.pageMarkNum = lineIndex[0];
				return;
			}
			i++;
			
		}
		
	
}

	//计算第零页
	
	public void parseFileZero(String fileNameStr, int lineCount) throws IOException {

			int readNum;
			int width = 0;
		
			String newString = null, oldString = new String("");
			int index = 0;
			boolean hasEnter = false;
			boolean hasEnoughWidth = false;
			fileFirstStart = true;
			int countCharNum = 0;
			System.out.println("--------while (true) {------end-----------");
			//moveSkip = (int)skipNumber + searchNumber;
			System.out.println("--------moveSkip {------end-----------"+moveSkip);
			
			int i = 0;
			lineIndex3[0] = 0;
			curLine3 = 0;
			while (true) {
				if(i == str.length()/MAX_BUF_SIZE){
					break;
				}
				newString = str.substring(i*MAX_BUF_SIZE, (i+1)*MAX_BUF_SIZE);
				// 读取一次的数据
				while (true) {
					newString = oldString + newString;
					oldString = new String("");
					width = 0;
					if (hasEnter == true){
						width = TextSizeUtil.fontSize;
					}
					hasEnter = false;
					hasEnoughWidth = false;
					
					for (index = 0; index < newString.length(); index++) {
						String text = newString.substring(index, index + 1);

						//if (newString.charAt(index) != '\r') {
							width += getCharWidth(text);
						//}
						// string 's width > screenWidth
						if (width > screenWidth - TextSizeUtil.fontSize) {
							hasEnoughWidth = true;
							break;
						}
						if (newString.charAt(index) == '\n') {
							hasEnter = true;
							break;
						}

						
					}

					// hasEnter || hasEnoughWidth
					
					if (hasEnter || hasEnoughWidth) {

						lineIndex3[curLine3 + 1] = lineIndex3[curLine3] + index + 1;

						curLine3++;
						index++;
						newString = newString.substring(index);

					} else {
						oldString = newString;
						break;
					}
				
					if (curLine3 >= lineCount) {
						
	
						return;
					}
				}
				i++;
			}
			
			return;
		}

	//获取第一页的数据
	
	public String getFirstPageContent(int lineNo, int lineCount){
			StringBuffer strBuffer = new StringBuffer();
			long skipNum, retNum;
			int readNum;
			String Str = "    ";
			String tempStr ;
			boolean lineEndFlag = false;
			int j = 0;
			
			if (lineNo >= curLine3)
				return content = null;
			
			// reset the stream
			
			// skip the stream count n char
			
			skipNum = lineIndex3[lineNo];
		
			//str = str.substring((int) skipNum);
			
			// get content
			
			for (int i = 0; i < lineCount; i++) {
			
				
				String subStr = null;
				
				if (lineNo + i +1> curLine3) {
					
					break;
				}
				
			//	for(int t = 0;t<=curLine;t++){
			//		System.out.println("lineIndex"+t+"="+lineIndex[t]);
			//	}
				subStr =  str.substring(lineIndex3[lineNo + i], lineIndex3[lineNo + i + 1]);
				readNum = str.substring(lineIndex3[lineNo + i], lineIndex3[lineNo + i + 1]).length();
				
				
				if (readNum == -1){
					break;
				}
			/*			if (buf[readNum - 1] == '\n') {
					lineEndFlag = true;
					// str += (new String(buf, 0, readNum));
					strBuffer.append(buf, 0, readNum);
				} else {
					if(lineEndFlag == true){
						
						
						strBuffer.append(Str);
						lineEndFlag = false;
					}else{
					// str += ((new String(buf, 0, readNum)) + '\n');
					strBuffer.append(buf, 0, readNum);
					strBuffer.append('\n');
					}
				}*/
				
				 if (subStr.charAt(readNum-1)=='\n'){
						//str += (new String(buf, 0, readNum));
						strBuffer.append(subStr);
					
			
						
					} else {
						//str += ((new String(buf, 0, readNum)) + '\n');
						strBuffer.append(subStr);
						strBuffer.append('\n');
					
					}
			
			}
			
			
			
			//pageEndPoint = lineIndex3[lineNo+lineCount-1];
			content = new String(strBuffer);
			content =content.replace("\r",""); 
			return content;
		}

	
}
